package sample.test.et{
	import sample.test.ConsoleManager;
	import com.takanafu.haruframework.d2l.util.ApplicationContext;
	import com.takanafu.haruframework.aop.reflect.MethodImpl;
	import flash.events.Event;
	import com.takanafu.haruframework.d2l.invoker.MethodInfo;
	import flash.net.URLRequest;
	import flash.net.URLLoader;
	import mx.controls.TextInput;
	
	public class EventHandleTest{
		private var cm:ConsoleManager;
		private var cnt:Number = 1;
		
		function EventHandleTest(){
			cm = ApplicationContext.getBean("consoleManager") as ConsoleManager;
		}
		
		private function initializeHandler(event:Event,method:MethodImpl=null):void{
			cm.println(cnt.toString()+"回目："+event.target.data);
		}
		
		private function initializeHandler2(event:Event,method:MethodImpl=null):MethodInfo{
			cm.println(cnt.toString()+"回目："+event.target.data);
			++cnt;
			
			var ret:MethodInfo = null;
			var mimpl:MethodImpl = method as MethodImpl;
			
			if(mimpl != null){
				var req:URLRequest = new URLRequest("text.txt");
				var loader:URLLoader = new URLLoader();
				
				loader.addEventListener(Event.COMPLETE,mimpl.invoker.handler);
				loader.load(req);
				ret = new MethodInfo(initializeHandler,MethodInfo.USE_HANDLER);
			}
			
			return ret;
		}
		
		private function initializeHandler3(event:Event,method:MethodImpl=null):void{
			cm.println(cnt.toString()+"回目："+event.target.data);
			
			throw new Error("わざとエラー[initializeHandler3]");
		}
		
		public function test(event:Event,method:MethodImpl=null):MethodInfo{
			cm.println("EventHandleTest::test が実行されました");
			
			var ret:MethodInfo = null;
			var mimpl:MethodImpl = method as MethodImpl;
			
			if(mimpl != null){
				var req:URLRequest = new URLRequest("text.txt");
				var loader:URLLoader = new URLLoader();
				
				loader.addEventListener(Event.COMPLETE,mimpl.invoker.handler);
				loader.load(req);
				ret = new MethodInfo(initializeHandler,MethodInfo.USE_HANDLER);
			}
			
			return ret;
		}
		
		public function test_callB(event:Event,method:MethodImpl=null):void{
			cm.println("EventHandleTest::test_callB が実行されました");
		}
		
		public function test2(event:Event,method:MethodImpl=null):MethodInfo{
			cm.println("EventHandleTest::test2 が実行されました");
			
			var ret:MethodInfo = null;
			var mimpl:MethodImpl = method as MethodImpl;
			
			if(mimpl != null){
				var req:URLRequest = new URLRequest("text.txt");
				var loader:URLLoader = new URLLoader();
				
				loader.addEventListener(Event.COMPLETE,mimpl.invoker.handler);
				loader.load(req);
				ret = new MethodInfo(initializeHandler2,MethodInfo.USE_HANDLER);
			}
			
			return ret;
		}
		
		public function test_callA(event:Event,method:MethodImpl=null):void{
			cm.println("EventHandleTest::test_callA が実行されました");
		}
		
		public function test_callT(event:Event,method:MethodImpl=null):void{
			cm.println("EventHandleTest::test_callT が実行されました");
			throw new Error("わざとエラー[test_callT]");
		}
		
		public function test_handle_callT(event:Event,method:MethodImpl=null):MethodInfo{
			cm.println("EventHandleTest::test_handle_callT が実行されました");
			
			var ret:MethodInfo = null;
			var mimpl:MethodImpl = method as MethodImpl;
			
			if(mimpl != null){
				var req:URLRequest = new URLRequest("text.txt");
				var loader:URLLoader = new URLLoader();
				
				loader.addEventListener(Event.COMPLETE,mimpl.invoker.handler);
				loader.load(req);
				ret = new MethodInfo(initializeHandler3,MethodInfo.USE_HANDLER);
			}
			
			return ret;
		}
				
		public function test3(event:Event,method:MethodImpl=null):void{
			cm.println("EventHandleTest::test3 が実行されました");
			
			var ti:TextInput = event.currentTarget as TextInput;
			
			if(ti != null){
				ti.text = "初期化が完了しました";
			}
		}
		
		public function test_callB_callA_callT(event:Event,method:MethodImpl=null):void{
			cm.println("EventHandleTest::test_callB_callA_callT が実行されました");
		}
	}
}